<?php

namespace App\Console\Commands;

use JonnyW\PhantomJs\Client;
use App\Jobs\DigSiteAiZhanJobs;
use Carbon\Carbon;
use QL\QueryList;
use Illuminate\Support\Facades\Cache;
use Illuminate\Console\Command;

class DigsiteAiZhan extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'dig:siteaizhan {col} {link}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'dig:siteaizhan';
    protected $pages    = [1,2,3,4,5];
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $link   = $this->argument('link');
        $col    = $this->argument('col');
        foreach ($this->pages as $v)
        {
            $url   = $link.'p'.$v.'.html';
            $data   = $this->check($url);
            if($data){
                $index  = $this->ithome($data);
                if($index){
                    $collection = collect($index);
                    $multiplied = $collection->map(function ($item) use ($col) {
                        DigSiteAiZhanJobs::dispatch($item,$col);
                    });
                    $all    = $multiplied->all();
                    $this->info('dig article ok!'.$url);
                }else{
                    $this->error('dig article result!'.$url);
                }
            }else{
                $this->error('dig article error!'.$url);
            }
            sleep(5);
        }
    }

    function ithome($data){
        $html = QueryList::html($data)->find('.wlist .main .clearfix .fl .bd .list');
        // 选择正文内容中要移除的元素，并移除
        //获取纯净的正文内容
        $ql = QueryList::html($html->html())->rules([
            //采集规则
            'link'  => ['.text h2 a','href'],
            'title' => ['.text h2 a','text'],
        ])->range('ul li')->query()->getData(function ($item){
            $item['link']	= $item['link'];
            return $item;
        });
        $data   = $ql->all();
        $collection = collect($data);
        if(!$collection->isEmpty()){
            $multiplied = $collection->map(function ($item, $k) use ($collection) {
                $key    = 'dig_site_info_aizhan_'.md5($item['link']);
                $value = Cache::get($key);
                if($value){
                    $collection->pull($k);
                }else{
                    $expiresAt = Carbon::now()->addMinutes(env('DIG_CACHE'));
                    Cache::put($key, $item, $expiresAt);
                    return $item;
                }
            });
            return $multiplied->all();;
        }else{
            return false;
        }
    }
    
    //抓取网址
    function check($url){
        
        $client = Client::getInstance();
        $client->isLazy(); // Tells the client to wait for all resources before rendering
        /** 
         * @see JonnyW\PhantomJs\Http\Request
         **/
        $request = $client->getMessageFactory()->createRequest($url, 'GET');
        $request->setTimeout(5000); // Will render page if this timeout is reached and resources haven't finished loading
        $request->setDelay(5);
    
        /** 
         * @see JonnyW\PhantomJs\Http\Response 
         **/
        $response = $client->getMessageFactory()->createResponse();
    
        // Send the request
        $client->send($request, $response);
    
        if($response->getStatus() === 200) {
            $html   = $response->getContent();
            $html    = preg_replace("/<[ ]+/si","<", $html); //过滤<("<"号后面带空格)
            $html    = preg_replace("/<\!–.*?–>/si","", $html); //注释
    		$html    = preg_replace("'<script[^>]*?>.*?</script>'si", '', $html);
    		$html    = preg_replace("/<(\/?head.*?)>/si", "", $html); //过滤head标签
    		$html    = preg_replace("/<(\/?meta.*?)>/si", "", $html); //过滤meta标签
            $html    = preg_replace("/<(\/?body.*?)>/si", "", $html); //过滤body标签
            $html    = preg_replace("/<(\/?link.*?)>/si", "", $html); //过滤link标签
            $html    = preg_replace("'<style[^>]*?>.*?</style>'si", '', $html);
            $html    = preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","", $html); //过滤title标签
            $html    = preg_replace("/<(\/?title.*?)>/si","", $html); //过滤title标签
            $html   = preg_replace("'<script[^>]*?>.*?</script>'si", '', $html);
            // Dump the requested page content
            return $html;
        } else {
            return false;
        }
        
    }
}
